from hanchlogger import logger
import openpyxl
import hanchxl
from openpyxl.cell import MergedCell
from openpyxl.utils import get_column_letter


class ChengBanInFo(object):
    def __init__(self, is_ke_shi):
        self.isKeShi = is_ke_shi
        self.name = ""
        self.fuZeRen = ""
        self.suoShuPianQu = ""
        self.baoPianLingDao = ""
        self.zhuGuanLingDao = ""
        self.qianRenSuQiu = 0
        self.fenDang = None
        self.muBiaoJieJue = 0
        self.muBiaoJianMian = 0


class LingDaoInfo(object):
    def __init__(self, xing_ming='', zhi_wu=''):
        self.name = xing_ming
        self.zhiWu = zhi_wu


class ChengBanDirectory(object):
    def __init__(self, data_only=True):
        self.fileName = ''
        self.dataOnly = data_only
        self.bookExcel = None
        self.sheetKeShi = None
        self.sheetSheQu = None
        self.sheetLingDao = None
        self.listKeShi = {}
        self.listSheQu = {}
        self.listLingDao = {}
        self.listKeShiArray = []
        self.listSheQuArray = []

    def get_cheng_ban_info(self, sheet_src, is_Ke_shi):
        maxRowSheet = sheet_src.max_row
        indexColZeRenQu = hanchxl.get_col_index(sheet_src, '责任区名称')
        indexColQianRen = hanchxl.get_col_index(sheet_src, '千人诉求量')
        indexColFuZeRen = hanchxl.get_col_index(sheet_src, '负责人')
        indexColSuoShuPianQu = hanchxl.get_col_index(sheet_src, '所属片区')
        indexColZhuGuan = hanchxl.get_col_index(sheet_src, '主管领导')
        indexColBaoPian = hanchxl.get_col_index(sheet_src, '包片领导')
        indexColFenDang = hanchxl.get_col_index(sheet_src, '分档')
        indexColMuBiaoJieJue = hanchxl.get_col_index(sheet_src, '解决率目标值')
        indexColMuBiaoJianMian = hanchxl.get_col_index(sheet_src, '见面率目标值')
        logger.debug('处理Sheet：{}  is_Ke_shi:{}  indexColFenDang:{}'.format(sheet_src.title, is_Ke_shi, indexColFenDang))
        for rowIndex in range(2, maxRowSheet + 1):
            chengBanInfo = ChengBanInFo(is_Ke_shi)
            chengBanBuMen = None
            if indexColZeRenQu is not None:
                chengBanBuMen = sheet_src.cell(rowIndex, indexColZeRenQu).value
            if chengBanBuMen is not None:
                chengBanInfo.name = chengBanInfo
                if indexColFuZeRen is not None:
                    if sheet_src.cell(rowIndex, indexColFuZeRen).value is not None:
                        chengBanInfo.fuZeRen = sheet_src.cell(rowIndex, indexColFuZeRen).value.strip()
                    else:
                        chengBanInfo.fuZeRen = ''
                if indexColSuoShuPianQu is not None:
                    if sheet_src.cell(rowIndex, indexColSuoShuPianQu).value is not None:
                        chengBanInfo.suoShuPianQu = sheet_src.cell(rowIndex, indexColSuoShuPianQu).value.strip()
                    else:
                        chengBanInfo.suoShuPianQu = ''
                if indexColQianRen is not None:
                    if sheet_src.cell(rowIndex, indexColQianRen).value is not None:
                        chengBanInfo.qianRenSuQiu = sheet_src.cell(rowIndex, indexColQianRen).value
                if indexColFenDang is not None:
                    if sheet_src.cell(rowIndex, indexColFenDang).value is not None:
                        chengBanInfo.fenDang = sheet_src.cell(rowIndex, indexColFenDang).value
                if indexColZhuGuan is not None:
                    if sheet_src.cell(rowIndex, indexColZhuGuan).value is not None:
                        chengBanInfo.zhuGuanLingDao = sheet_src.cell(rowIndex, indexColZhuGuan).value.strip()
                    else:
                        chengBanInfo.zhuGuanLingDao = ''
                if indexColBaoPian is not None:
                    if sheet_src.cell(rowIndex, indexColBaoPian).value is not None:
                        chengBanInfo.baoPianLingDao = sheet_src.cell(rowIndex, indexColBaoPian).value.strip()
                    else:
                        chengBanInfo.baoPianLingDao = ''
                if indexColMuBiaoJieJue is not None:
                    if sheet_src.cell(rowIndex, indexColMuBiaoJieJue).value is not None:
                        chengBanInfo.muBiaoJieJue = sheet_src.cell(rowIndex, indexColMuBiaoJieJue).value
                if indexColMuBiaoJianMian is not None:
                    if sheet_src.cell(rowIndex, indexColMuBiaoJianMian).value is not None:
                        chengBanInfo.muBiaoJianMian = sheet_src.cell(rowIndex, indexColMuBiaoJianMian).value
                if is_Ke_shi is True:
                    self.listKeShi[chengBanBuMen.strip()] = chengBanInfo
                    self.listKeShiArray.append(chengBanBuMen.strip())
                else:
                    self.listSheQu[chengBanBuMen.strip()] = chengBanInfo
                    self.listSheQuArray.append(chengBanBuMen.strip())

    def get_ling_dao_info(self, sheet_boss):
        maxRowLingDao = sheet_boss.max_row
        indexColZhiWu = hanchxl.get_col_index(sheet_boss, '职务')
        indexColName = hanchxl.get_col_index(sheet_boss, '姓名')
        logger.debug('处理Sheet：{}  '.format(sheet_boss.title))
        for rowIndex in range(2, maxRowLingDao + 1):
            lingDaoInfo = LingDaoInfo()
            if indexColName is not None:
                bossName = sheet_boss.cell(rowIndex, indexColName).value
                if bossName is not None:
                    lingDaoInfo.name = bossName.strip()
                    if indexColZhiWu is not None:
                        lingDaoInfo.zhiWu = sheet_boss.cell(rowIndex, indexColZhiWu).value.strip()
                    self.listLingDao[bossName.strip()] = lingDaoInfo

    def load_cheng_ban_list(self, file_name):
        self.bookExcel = openpyxl.load_workbook(file_name, data_only=self.dataOnly)
        self.sheetKeShi = self.bookExcel['承办部门-科室']
        maxRowKeShi = self.sheetKeShi.max_row
        maxColKeShi = self.sheetKeShi.max_column
        logger.debug('承办部门-科室 : {} 行 ， {} 列'.format(maxRowKeShi, maxColKeShi))
        self.get_cheng_ban_info(self.sheetKeShi, True)

        self.sheetSheQu = self.bookExcel['承办部门-社区村']
        maxRowSheQu = self.sheetSheQu.max_row
        maxColSheQu = self.sheetSheQu.max_column
        logger.debug('承办部门-社区村 : {} 行 ， {} 列'.format(maxRowSheQu, maxColSheQu))
        self.get_cheng_ban_info(self.sheetSheQu, False)

        self.sheetLingDao = self.bookExcel['机关领导']
        maxRowLingDao = self.sheetLingDao.max_row
        maxColLingDao = self.sheetLingDao.max_column
        logger.debug('机关领导 : {} 行 ， {} 列'.format(maxRowLingDao, maxColLingDao))
        self.get_ling_dao_info(self.sheetLingDao)

    def get_list_ke_shi(self):
        return self.listKeShi

    def get_list_she_qu(self):
        return self.listSheQu

    def get_list_ke_shi_array(self):
        return self.listKeShiArray

    def get_list_she_qu_array(self):
        return self.listSheQuArray

    def get_list_ling_dao(self):
        return self.listLingDao

